#include "stc8h.h"
#include "../KEY/KEY.h"
#include "../main/configuration.h"

/*****************************宏定义**********************************/



#define key_state_0 0   //按键判断状态0
#define key_state_1 1   //按键判断状态1     消抖态
#define key_state_2 2   //按键判断状态2     长按确认态
#define key_state_3 3   //按键判断状态3     长按释放态

/***********************变量定义**************************************/



/*按键检测FIFO流程

read   write   分别记录当前索引值，读取或写入完成累加，大于队列长度索引复原，read!=write则有按键事件

队列长度8
*/
struct keydetct key_0 =
{
    0,
    0,
    {0,0,0,0,0,0,0,0}

};

/****************************函数定义************************************************/


unsigned char read_key()
{
    unsigned char data key_status;
    if(key_0.read!=key_0.write)
    {
        key_status=key_0.buf[key_0.read];
        key_0.read++;
        if(key_0.read>7)
        {
            key_0.read=0;
        }
        
    }else{key_status=0;}    
    return key_status;
}



void write_key(unsigned char data key_current_status)
{
    if(key_current_status)
    {
        key_0.buf[key_0.write]=key_current_status;
        if(++key_0.write>7){key_0.write=0;}
    }
}


unsigned char key_driver_low()                                   //底层驱动
{
    static unsigned char data key_state=key_state_0,data key_time=0;
    unsigned char data key_press,data key_return=n_key;
    key_press=!key_input;
    switch (key_state)
    {
    case key_state_0:
        if(key_press)
        {
            key_state=key_state_1;                  //按键消抖态
        }
        break;
    
    case key_state_1:
        if(key_press)
        {
            key_time=0;
            key_state=key_state_2;
        }
        else{
            key_state=key_state_0;
        }
        break;
        case key_state_2:                           //单击/长按确认态
        
            if(!key_press)
            {
                key_return=s_tap;
                key_state=key_state_0;

            }else if(++key_time>=100)
            {
                key_return=l_press;
                key_state=key_state_3;                  //等待按键释放态
            }
            break;
        case key_state_3:
        
            if(!key_press){key_state=key_state_0;}          //长按释放
        
            break;
    }
    return key_return;
}
unsigned char key_driver_high()                                                   //上层驱动
{
    static unsigned char data key_detect_state=key_state_0,data key_detect_time=0;
    unsigned char data key_detect_return=n_key,data key_detect_temp;
    switch (_10ms_ok)
    {
    case 0:
        
        break;
    
    case 1:
        _10ms_ok=0;                                         //标志位清零
        key_detect_temp=key_driver_low();
        
    
        switch (key_detect_state)
        {
        case key_state_0:
            if(key_detect_temp==s_tap)
            {
                key_detect_time=0;                          //开始计时
                key_detect_state=key_state_1;               //进入判断态

            }else{
                key_detect_return=key_detect_temp;
            }
            break;
        
        case key_state_1:
            if(key_detect_temp==s_tap)
            {
                key_detect_return=d_tap;
                key_detect_state=key_state_0;

            }else if(++key_detect_time>=13)
            {
                key_detect_return=s_tap;
                key_detect_state=key_state_0;
            }
            
            break;
        }
        break;
    }

    return key_detect_return;
}



void key_detect()                       //按键检测（fifo）
{
    unsigned char data key_fifo_temp=key_driver_high();
    write_key(key_fifo_temp);
}
